//
//  ImageScrollView.m
//  NMPlus
//
//  Created by  on 16/11/2011.
//  Copyright (c) 2011 广东薪传 . All rights reserved.
//

#import "ImageScrollView.h"

@implementation ImageScrollView
@synthesize imageView;
- (id)initWithFrame:(CGRect)frame imageView:(UIImageView*)_imageView{
    if ((self = [super initWithFrame:frame])) {
		
		// Set up the UIScrollView
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.bouncesZoom = YES;
        self.decelerationRate = UIScrollViewDecelerationRateNormal;
        self.delegate = self;
		[self setBackgroundColor:[UIColor clearColor]];
		self.maximumZoomScale = 5.0;
		self.minimumZoomScale = 1.0;
        self.imageView=_imageView;
		//self.contentSize=CGSizeMake(image.size.width+3, image.size.height+2);
        CGRect rect=[[UIScreen mainScreen] bounds];
        self.contentSize=CGSizeMake(rect.size.width,rect.size.height);
       // self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
       // imageView.image=image;
		CGRect pageRect = imageView.frame;
        if (pageRect.size.width>0) {
            pdfScale = self.frame.size.width/pageRect.size.width;
            pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
            imageView.contentMode=UIViewContentModeScaleAspectFit;
            imageView.frame=pageRect;
            [self addSubview:imageView];
        }

    }
    return self;
}

- (void)dealloc
{
	// Clean up
    [imageView release];
    
    [super dealloc];
}

#pragma mark -
#pragma mark Override layoutSubviews to center content

// We use layoutSubviews to center the PDF page in the view
- (void)layoutSubviews 
{
    [super layoutSubviews];
    
    // center the image as it becomes smaller than the size of the screen
	
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = imageView.frame;
    
    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;
    
    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;
    
    imageView.frame = frameToCenter;
    
	// to handle the interaction between CATiledLayer and high resolution screens, we need to manually set the
	// tiling view's contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 on high resolution screens,
	// which would cause the CATiledLayer to ask us for tiles of the wrong scales.)
	imageView.contentScaleFactor = 1.0;
}

#pragma mark -
#pragma mark UIScrollView delegate methods

// A UIScrollView delegate callback, called when the user starts zooming. 
// We return our current TiledPDFView.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return imageView;
}


- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
	pdfScale *=scale;
	CGRect pageRect =imageView.frame;
	pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
   // imageView.frame=pageRect;
	[self addSubview:imageView];
}


- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{

	[oldImageView removeFromSuperview];
	//[oldImageView release];
	oldImageView = imageView;
	[self addSubview:oldImageView];
}

@end
